<layout name="../Common/Master/master" />
<div id="content">
	<form action="" onsubmit="return pay()" method="post">
		<div class="car main">
			<div class="carshop">
				<div class="cartitle">
					<div class="carcheck">
						<div class="checkbox">
							<span class="check checkon" id="allcheck"></span>
						</div>
						全选</div>
					<div class="carname">商品名称</div>
					<div class="carmoney">单价</div>
					<div class="carnum">数量</div>
					<div class="carcount">小计</div>
					<div class="carhandle">操作</div>
				</div>
				<foreach name="goods" item="vo">
				<div class="shopcontent">
					<input type="hidden" disabled name="gid[]" value="{$vo['gid']}">
					<input type="hidden" disabled name="pcid[]" value="{$vo['pcid']}">
					<div class="shopcheck">
						<div class="checkbox">
						<span class="check checkon" ch="1"></span>
						<input type="checkbox"  class="checkhide"/>

					     </div>
					</div>
					<div class="shopname" >
						<div class="carimg">
							<a href=""><img width="50px" height="70px" src="{$vo['thumb']}"/></a>
						</div>
						<p>
							<a href="#" style="font-size: 14px">{$vo['gname']}&nbsp;&nbsp;</a>
							<span>{$vo['digest']}||{$vo['pstyle']}</span>
						</p>
					</div>
					<div class="shopmoney">{$vo['priced']}</div>
					<div class="shopnum">
						<a href="javascript:;" gid="{$vo['gid']}" pcid="{$vo['pcid']}" class="num_l">-</a>
						<input class="count" type="text" readonly value="{$vo['count']}" />
						<a href="javascript:;" gid="{$vo['gid']}" pcid="{$vo['pcid']}" stock="{$vo['stock']}" class="num_r">+</a>
					</div>
					<div class="shopcount">元</div>
					<div class="shophandle"><span  pcid="{$vo['pcid']}" gid="{$vo['gid']}" >x</span></div>
				</div>
				</foreach>
			</div>
			<div class="jiesuan">
				<div class="jixu"><a href="">继续购物</a></div>
				<div class="gongji">共计<span>10</span>件商品</div>
				<div class="heji">合计<span>100.00</span>元</div>
				<div class="gou"><input type="submit" value="去结算"/></div>
			</div>
			<script>
				$(function(){
					//防止用户一直点+
					var sta=1;
					setInterval(move,2000);
					function move(){
                       sta=0;
						setTimeout(function(){
							sta=1;
						},2000);
					}
					//点击删除商品时
					$('.shophandle span').click(function(){
						var gid=$(this).attr('gid');
						var pcid=$(this).attr('pcid');
//						alert(gid);
						$.post("{:U('home/car/delgood')}",{gid:gid,pcid:pcid},function(res){

						},'json');
					})
					//计算总价格，总数量
					var zongjia=0;
					var zongshu=0;
					for(var i=0;i<$(".shopcontent").length;i++){
                       var price=parseFloat($('.shopcontent').eq(i).find('.shopmoney').text());
						var count=parseInt($('.shopcontent').eq(i).find('.count').val());
						var jiage=price*count;
						$('.shopcount').eq(i).text(jiage);
						zongjia=zongjia+jiage;
						zongshu=zongshu+count;
//						alert(zongshu);
					}
					$('.jiesuan').find('.heji').find('span').text(zongjia);
					$('.jiesuan').find('.gongji').find('span').text(zongshu);

					//点击加的时候小计同步增加
					$('.car .carshop .shopcontent .shopnum a.num_r').click(function(){
//                         alert(sta);
						//获取该商品的gid
						var gid=parseInt($(this).attr('gid'));
						//获取该商品的pcid
						var pcid=parseInt($(this).attr('pcid'));
						var stock=parseInt($(this).attr('stock'));
//						alert (stock);
						if(sta){
							var danjia=parseInt($(this).closest('.shopnum').prev().text());
							var count=parseInt($(this).closest('.shopnum').next().text());
							var num=$(this).prev().val();
							if(num>=0){
								num++;
								$(this).prev().val(num);
								//$(this).closest('.shopnum').next().text(num*danjia+'元');
								//$('.heji span').text(setcount().n+'元');
							}
							if(num>stock){
								alert('库存不够了');
								$(this).prev().val(stock);
								//$(this).closest('.shopnum').next().text(100*danjia+'元');
							}
//							alert(sta);
							aa=parseFloat($(this).parent().siblings('.shopcount').text())+ parseFloat($(this).parent().siblings('.shopmoney').text());
							aa=aa.toFixed(2);
							$(this).parent().siblings('.shopcount').text(aa);
							//总价增加
							zongjia=zongjia+parseFloat($(this).parent().siblings('.shopmoney').text());
							$('.jiesuan').find('.heji').find('span').text(zongjia);
							//总数量增加
							zongshu=zongshu+1;
//						alert(2);
//						alert(zongshu);
							$('.jiesuan').find('.gongji').find('span').text(zongshu);
							//发异步，将数据库商品的数量加1
							$.post("{:U('home/car/ajaxAddgood')}",{gid:gid,pcid:pcid}, function (res) {

							},'json');
						}
						sta=0;

					});


                    //点击减的时候小计同步减少
					$('.car .carshop .shopcontent .shopnum a.num_l').click(function(){
						var counts=parseInt($(this).siblings('.count').val());
						if(counts<=1){
                          return false;
						};

						var danjia=parseInt($(this).closest('.shopnum').prev().text());
						var count=parseInt($(this).closest('.shopnum').next().text());
						var num=$(this).next().val();
						if(num<=1){
							alert('小于1咋买啊');
							$(this).next().val(1);
						}
						if(num>1){
							num--;
							$(this).next().val(num);
						}
						aa=parseFloat($(this).parent().siblings('.shopcount').text())- parseFloat($(this).parent().siblings('.shopmoney').text());
						aa=aa.toFixed(2);
						$(this).parent().siblings('.shopcount').text(aa);
						//总价减少
						zongjia=zongjia-parseFloat($(this).parent().siblings('.shopmoney').text());
//						zongjia=zongjia.toFixed(2);
						$('.jiesuan').find('.heji').find('span').text(zongjia);
						//总数量减少
						zongshu=zongshu-1;
						$('.jiesuan').find('.gongji').find('span').text(zongshu);
						//发异步将用户数据库中的该商品的数量减1
						//获取该商品的gid
						var gid=parseInt($(this).attr('gid'));
						//获取该商品的pcid
						var pcid=parseInt($(this).attr('pcid'));
						$.post("{:U('home/car/ajaxreduce')}",{gid:gid,pcid:pcid},function(res){

						},'json');
					});
//					alert(zongjia);
					//计算多少件商品
				})
			</script>
		</div>
	</form>
</div>

<script>
	//点击去结算时
	function pay(){
//		alert(333);
		 var count=$(".checkbox .check[ch='1']").length;
		if(count==0){
			layer.msg('请选择要付款的商品', function(){
			});
			return false;
		}
		$.each($(".checkbox .check[ch='1']"),function(){
//			alert(33);
			$(this).parents('.shopcontent').find('input').removeAttr('disabled');
		})
		var data=$('form').serialize();
//		alert(data.unseri);
//		location.href="__ROOT__/index.php/home/order/pay/data/"+data;
		$.post("{:U('home/order/pay')}",data,function(res){
          if(res){
			  location.href="{:U('home/order/pay',['paystatus'=>1])}";
		  }
		});

 		return false;
	}

</script>